package com.ruoyi.utils;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.ruoyi.domain.MFile;
import com.ruoyi.mapper.MFileMapper;
import com.ruoyi.platform.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;

/**
 * 通用文件处理工具类
 */
@Component
@Slf4j
public class BaseFileUtils {

    @Autowired
    private MFileMapper mFileMapper;

    @Cached(name = "base:file:",key = "#fileId",cacheType = CacheType.BOTH)
    public String getFileUrlById(@NotNull String fileId){
        if (StringUtil.isEmpty(fileId)){
            return null;
        }
        MFile mFile = mFileMapper.selectById(fileId);
        if (mFile !=null) {
            return mFile.getFileUrl();
        }
        log.debug("根据文件Id：{}，查询数据为空",fileId);
        return null;
    }

    @Cached(name = "base:file:originName:",key = "#fileId",cacheType = CacheType.BOTH)
    public String getFileOriginName(@NotNull String fileId){
        if (StringUtil.isEmpty(fileId)){
            return null;
        }
        MFile mFile = mFileMapper.selectById(fileId);
        if (mFile !=null) {
            return mFile.getOriginName();
        }
        log.debug("根据文件Id：{}，查询数据为空",fileId);
        return null;
    }

}
